package com.huawei.bus.minitest.conf;


import java.io.IOException;
import java.io.InputStream;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.huawei.bus.minitest.constant.ConfigConstant;
import com.huawei.bus.minitest.util.FileUtils;

public class Conf {
	
	public static GlobConf glob;
	public static SMAPConf[] smaps;
	public static DatabaseConf[] userdbs;
	public static DatabaseConf sysdb;
	public static DatabaseConf[] cbpdbs;
	public static DatabaseConf scpdb;
	
	public static void init() throws JSONException {
		InputStream is = null;
		String str = null;
		try {
			is = Conf.class.getClassLoader().getResourceAsStream(ConfigConstant.CONF_JSON_PATH);
			str = FileUtils.readFileFromInputStream(is);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		JSONObject jsonObj = new JSONObject(str);
		
		initGlob(jsonObj);
		initSMAPs(jsonObj);
		initDatabase(jsonObj);
	}

	private static void initGlob(JSONObject jsonObj) throws JSONException {
		JSONObject gloObj = jsonObj.getJSONObject(ConfigConstant.GLOB);
		glob = new GlobConf();
		glob.setTestsuitePath(gloObj.getString(ConfigConstant.TEST_SUITES_PATH));
	}

	private static void initDatabase(JSONObject jsonObj) throws JSONException {
		JSONObject dbObj = jsonObj.getJSONObject(ConfigConstant.DB);
		
		//init userdb
		JSONArray jsUserdb = dbObj.getJSONArray(ConfigConstant.USERDB);
		if(jsUserdb != null && jsUserdb.length()>0)
		{
			userdbs = new DatabaseConf[jsUserdb.length()];
			for(int i=0;i<jsUserdb.length();i++){
				JSONObject current = jsUserdb.getJSONObject(i);
				userdbs[i] = getDatabaseConf(current);
			}
		}
		
		//init sysdb
		JSONObject jsSysdb = dbObj.getJSONObject(ConfigConstant.SYSDB);
		if(jsSysdb != null)
		{
			sysdb = getDatabaseConf(jsSysdb);
		}
		
		//init cbpdb
		JSONArray jscbpdb = dbObj.getJSONArray("cbpdb");
		if(jscbpdb != null && jscbpdb.length()>0)
		{
			cbpdbs = new DatabaseConf[jscbpdb.length()];
			for(int i=0;i<jscbpdb.length();i++){
				JSONObject current = jscbpdb.getJSONObject(i);
				cbpdbs[i] = getDatabaseConf(current);
			}
		}
	}

	private static DatabaseConf getDatabaseConf(JSONObject current)
			throws JSONException {
		DatabaseConf db = new DatabaseConf();
		db.setDbtype(current.getString(ConfigConstant.DBTYPE));
		db.setPassword(current.getString(ConfigConstant.PASSWORD));
		db.setServicename(current.getString(ConfigConstant.SERVICENAME));
		db.setUrl(current.getString(ConfigConstant.DB_URL));
		db.setUsername(current.getString(ConfigConstant.USERNAME));
		return db;
	}

	private static void initSMAPs(JSONObject jsonObj) throws JSONException {
		JSONArray jsArr = jsonObj.getJSONArray(ConfigConstant.SMAPS);
		if(jsArr != null && jsArr.length()>0){
			smaps = new SMAPConf[jsArr.length()];
			for(int i=0; i<jsArr.length(); i++){
				JSONObject current = jsArr.getJSONObject(i);
				SMAPConf smap = new SMAPConf();
				smap.setName(current.getString(ConfigConstant.SMAP_NAME));
				smap.setUrl(current.getString(ConfigConstant.SMAP_URL));
				smaps[i] = smap;
			}
		}
	}
	
}
